import request from '@/utils/request';
import type { 
  Activity, 
  ActivityQuery, 
  Order, 
  OrderQuery, 
  StatisticsData, 
  VerifyRecord, 
  VerifyQuery, 
  VerifyListData 
} from '@/types/groupbuy';

// 团购活动相关API
export const activityApi = {
  /**
   * 获取团购活动列表
   */
  getList: (params: ActivityQuery) => {
    return request<{ list: Activity[]; total: number }>({
      url: '/v1/groupbuy/activity',
      method: 'GET',
      data: params,
    });
  },

  /**
   * 获取团购活动详情
   */
  getDetail: (id: number) => {
    return request<Activity>({
      url: `/v1/groupbuy/activity/${id}`,
      method: 'GET',
    });
  },

  /**
   * 创建团购活动
   */
  create: (data: Partial<Activity>) => {
    return request({
      url: '/v1/groupbuy/activity',
      method: 'POST',
      data,
    });
  },

  /**
   * 更新团购活动
   */
  update: (id: number, data: Partial<Activity>) => {
    return request({
      url: `/v1/groupbuy/activity/${id}`,
      method: 'PUT',
      data,
    });
  },

  /**
   * 删除团购活动
   */
  delete: (id: number) => {
    return request({
      url: `/v1/groupbuy/activity/${id}`,
      method: 'DELETE',
    });
  },
};

// 团购订单相关API
export const orderApi = {
  /**
   * 获取团购订单列表
   */
  getList: (params: OrderQuery) => {
    return request<{ list: Order[]; total: number }>({
      url: '/v1/groupbuy/order',
      method: 'GET',
      data: params,
    });
  },

  /**
   * 获取团购订单详情
   */
  getDetail: (id: number) => {
    return request<Order>({
      url: `/v1/groupbuy/order/${id}`,
      method: 'GET',
    });
  },

  /**
   * 更新订单状态
   */
  updateStatus: (id: number, status: number) => {
    return request({
      url: `/v1/groupbuy/order/${id}/status`,
      method: 'PUT',
      data: { status },
    });
  },
};

// 统计分析相关API
export const statisticsApi = {
  /**
   * 获取统计数据
   */
  getData: (params: { startTime?: string; endTime?: string }) => {
    return request<StatisticsData>({
      url: '/v1/groupbuy/statistics',
      method: 'GET',
      data: params,
    });
  },

  /**
   * 获取销售趋势数据
   */
  getTrend: (params: { startTime?: string; endTime?: string; type?: string }) => {
    return request<any[]>({
      url: '/v1/groupbuy/statistics/trend',
      method: 'GET',
      data: params,
    });
  },
};

// 核销管理相关API
export const verifyApi = {
  /**
   * 获取核销记录列表
   */
  getList: (params: VerifyQuery) => {
    return request<VerifyListData>({
      url: '/v1/groupbuy/verify',
      method: 'GET',
      data: params,
    });
  },

  /**
   * 获取核销记录详情
   */
  getDetail: (id: number) => {
    return request<VerifyRecord>({
      url: `/v1/groupbuy/verify/${id}`,
      method: 'GET',
    });
  },

  /**
   * 扫码核销
   */
  scanVerify: (code: string) => {
    return request({
      url: '/v1/groupbuy/verify/scan',
      method: 'POST',
      data: { code },
    });
  },

  /**
   * 手动核销
   */
  manualVerify: (orderNo: string, remark?: string) => {
    return request({
      url: '/v1/groupbuy/verify/manual',
      method: 'POST',
      data: { orderNo, remark },
    });
  },
};

// 通用API
export const commonApi = {
  /**
   * 获取门店列表
   */
  getStores: () => {
    return request<{ list: any[] }>({
      url: '/v1/store',
      method: 'GET',
    });
  },

  /**
   * 获取场地列表
   */
  getVenues: (storeId?: number) => {
    return request<{ list: any[] }>({
      url: '/v1/venue',
      method: 'GET',
      data: storeId ? { store_id: storeId } : {},
    });
  },

  /**
   * 上传图片
   */
  uploadImage: (file: File) => {
    const formData = new FormData();
    formData.append('file', file);
    return request<{ url: string }>({
      url: '/v1/upload',
      method: 'POST',
      data: formData,
    });
  },
};